/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_referers.h"
#include "rep_logs_pages.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_top_ref_html_file(GraphFrame *gf,FILE *fp,int nref,WebStatPtr msn);
void
write_body_top_url_ref_html_file(GraphFrame *gf,FILE *fp,int nref,WebStatPtr msn);

int 
report_top_referers(GraphFrame *gf, char *name_file,int nref,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Top Host and Search Engine Referers ");
  write_start_body_html_file(fp,"#ffffff");
  write_body_top_ref_html_file(gf,fp,nref,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_top_ref_html_file(GraphFrame *gf,FILE *fp,int nref,WebStatPtr msn)
{
  LNode_type *lsites_ref = LOGR_get_list_sites_ref(msn);
  LNode_type *ptr = NULL;
  WebSiteNodePtr *varray = NULL;
  WebSiteNodePtr *varray2 = NULL;
  int i=0,j=0;
  char ntitle[100];
  int tn=0;
  int tnrefhost = 0;
  int tnrefsengine = 0;
  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;

  for(ptr = lsites_ref->back; ptr != lsites_ref; ptr=ptr->back)
  {
    WebSiteNodePtr msite = (WebSiteNodePtr)ptr->info;
    TypeReferer tr = LOGR_get_site_type_referer(msite);
    if(tr & RDYNAMIC_REF) 
	tnrefsengine++;
    if (tr == RNOREFERER)
	tnrefhost++;
    if (tr & RSTATIC_REF)
	tnrefhost++;
  }

  varray = tnrefhost ? (WebSiteNodePtr *)calloc(tnrefhost,sizeof(WebSiteNodePtr)) : NULL;

  if(!varray && tnrefhost > 0) {
      fprintf(stderr,"Out of Memory write_body_top_ref_html_file\n");
      abort();
  }

  varray2 = tnrefsengine ? (WebSiteNodePtr *)calloc(tnrefsengine,sizeof(WebSiteNodePtr)) : NULL;

  if(!varray2 && tnrefsengine > 0) {
      fprintf(stderr,"Out of Memory write_body_top_ref_html_file\n");
      abort();
  }

  for(ptr = lsites_ref->back; ptr != lsites_ref; ptr=ptr->back)
  {
    WebSiteNodePtr msite = (WebSiteNodePtr)ptr->info;
    TypeReferer tr = LOGR_get_site_type_referer(msite);
    if(tr & RDYNAMIC_REF)
	varray2[j++] = msite;
    if (tr == RNOREFERER)
	varray[i++] = msite;
    if (tr & RSTATIC_REF)
	varray[i++] = msite;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Top Host and Search Engine Referers");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique Host Referers : ");
  fprintf(fp, " %d Hosts\n",tnrefhost);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Unique Search Engine Referers : ");
  fprintf(fp, " %d Hosts\n",tnrefsengine);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RTHRString);
  write_horizontal_rule_html_file(fp);

  if(tnrefhost > 0) {
      
  if(nref < tnrefhost)
      tn = nref;
  else
      tn = tnrefhost;

  sprintf(ntitle,"%d Top Host Referers", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string5(fp,"Num","Host","Access Count",
			     "Percentage Access Count",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebSiteNodePtr msite= (WebSiteNodePtr)
	randomized_select((GPTR *)varray,i-1,tnrefhost-1,1,
		  LOGR_is_access_stat_lower,LOGR_is_access_stat_bigger);
    if(msite)
    {
	const char *lsite = LOGR_get_site_dns(msite);
	int scount = (int)LOGR_get_site_naccesses(msite);
	int scount_search =(int)LOGR_get_site_nacc_search(msite);
	char snum[20],str[20], str2[20], str3[20], str4[20];
	long ssize = LOGR_get_site_sizebytes(msite);
	long ssize_search = LOGR_get_site_sizebytes_search(msite);
	double ave_size = (double)((ssize-ssize_search)/tsize)*100.0;
	float ave_nacc = ((float)(scount-scount_search)/(float)tacc)*100.0;
	sprintf(snum,"%d",i);
	sprintf(str,"%d",scount-scount_search);
	sprintf(str2,"%.2f K",(float)((ssize-ssize_search)/1024.0));
	sprintf(str3,"%.2f %s",(float)(ave_size),"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_num_link_string4(fp,snum,lsite,str,str4,str2,str3);
    }
  }
  write_end_table_html_file(fp); }


  if(tnrefsengine > 0) {
      
  if(nref < tnrefsengine)
      tn = nref;
  else
      tn = tnrefsengine;

  sprintf(ntitle,"%d Top Search Engines Referers", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string5(fp,"Num","Search Engine","Access Count",
			     "Percentage Access Count",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebSiteNodePtr msite= (WebSiteNodePtr)
	randomized_select((GPTR *)varray2,i-1,tnrefsengine-1,1,
			  LOGR_is_access_search_lower,
			  LOGR_is_access_search_bigger);
    if(msite)
    {
	const char *lsite = LOGR_get_site_dns(msite);
	int scount = (int)LOGR_get_site_nacc_search(msite);
	char snum[20],str[20], str2[20], str3[20], str4[20];
	long ssize = LOGR_get_site_sizebytes_search(msite);
	double ave_size = (double)(ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(snum,"%d",i);
	sprintf(str,"%d",scount);
	sprintf(str2,"%.2f K",(float)(ssize/1024.0));
	sprintf(str3,"%.2f %s",(float)(ave_size),"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_num_link_string4(fp,snum,lsite,str,str4,str2,str3);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
}



int 
report_top_url_referers(GraphFrame *gf, char *name_file,int nref,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Top URL Referers ");
  write_start_body_html_file(fp,"#ffffff");
  write_body_top_url_ref_html_file(gf,fp,nref,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_top_url_ref_html_file(GraphFrame *gf,FILE *fp,int nref,WebStatPtr msn)
{
  LNode_type *lurl_ext_ref = LOGR_get_list_url_external_referers(msn);
  LNode_type *lurl_int_ref = LOGR_get_list_url_internal_referers(msn);
  LNode_type *ptr = NULL;
  WebIndependentPagePtr *varray3 = NULL;
  WebGenPagePtr *varray4 = NULL;
  int i=0;
  char ntitle[100];
  int tn=0;
  int tnurlextref = lurl_ext_ref ? count_llist(lurl_ext_ref) : 0;
  int tnurlintref = lurl_int_ref ? count_llist(lurl_int_ref) : 0;

  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;

  varray3 = tnurlextref ? (WebIndependentPagePtr *)calloc(tnurlextref,sizeof(WebIndependentPagePtr)) : NULL;

  if(!varray3 && tnurlextref > 0) {
      fprintf(stderr,"Out of Memory write_body_top_ref_html_file\n");
      abort();
  }

  varray4 = tnurlintref ? (WebGenPagePtr *)calloc(tnurlintref,sizeof(WebGenPagePtr)) : NULL;

  if(!varray4 &&  tnurlintref > 0) {
      fprintf(stderr,"Out of Memory write_body_top_ref_html_file\n");
      abort();
  }

  i=0;
  for(ptr = lurl_ext_ref->back; ptr != lurl_ext_ref; ptr = ptr->back)
  {
    WebIndependentPagePtr murle = (WebIndependentPagePtr)ptr->info;
    varray3[i++] = murle;
  }

  i=0;
  for(ptr = lurl_int_ref->back; ptr != lurl_int_ref; ptr = ptr->back)
  {
    WebGenPagePtr murli = (WebGenPagePtr)ptr->info;
    varray4[i++] = murli;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Top URL Referers");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique External URL Referers : ");
  fprintf(fp, " %d URLs\n",tnurlextref);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Unique Internal URL Referers : ");
  fprintf(fp, " %d URLs\n",tnurlintref);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RTURString);
  write_horizontal_rule_html_file(fp);

  if(tnurlextref > 0) {
  if(nref > tnurlextref)
      tn = tnurlextref;
  else
      tn = nref;

  sprintf(ntitle,"%d Top External URL Referers", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num", "URL","Count","Percentage");
  for(i = 1; i<=tn ; i++)
  {
    WebIndependentPagePtr opn = (WebIndependentPagePtr)randomized_select((GPTR *)varray3
					     ,i-1,tnurlextref-1,1,
				LOGR_is_access_ip_lower,LOGR_is_access_ip_bigger);
    if(opn)
    {
      const char *url = LOGR_get_ip_url(opn);
      WebGenPagePtr gpn = (WebGenPagePtr)LOGR_get_ip_info(opn);
      const char *turl = gpn ? LOGR_get_gen_url(gpn) : NULL;
      const char *ttit = gpn ? LOGR_get_gen_title(gpn) : NULL;
      long nacc = LOGR_get_ip_naccesses(opn);
      float ave_nacc = ((float)nacc/(float)tacc)*100.0;
      char snum[20],scount[20],save[20];
      sprintf(snum,"%d",i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      /*char *tit = (char *)get_attribute(v,"title");*/
      write_row_num_hyperlink_string2(fp,snum,url,NULL,turl,ttit,NULL,
				      scount,save);
    }
  }
  write_end_table_html_file(fp); }


  if(tnurlintref > 0) {
  if(nref > tnurlintref)
      tn = tnurlintref;
  else
      tn = nref;

  sprintf(ntitle,"%d Top Internal URL Referers", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Num", "URL","Count","Percentage");
  for(i = 1; i<=tn ; i++)
  {
    WebGenPagePtr gpn = (WebGenPagePtr)randomized_select((GPTR *)varray4
					     ,i-1,tnurlintref-1,1,
			 LOGR_is_access_gen_lower,LOGR_is_access_gen_bigger);
    if(gpn)
    {
      const char *url = LOGR_get_gen_url(gpn);
      long nacc = LOGR_get_gen_naccesses(gpn);
      float ave_nacc = ((float)nacc/(float)tacc)*100.0;
      char *tit = (char *)LOGR_get_gen_title(gpn);
      char snum[20],scount[20],save[20];
      sprintf(snum,"%d",i);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      write_row_num_link_title_string2(fp,snum,url,tit,scount,save);
    }
  }
  write_end_table_html_file(fp); }

  free(varray3); varray3 = NULL;
  free(varray4); varray4 = NULL;
}



